//
//  HSSegmentController.m
//  CheFangTimes
//
//  Created by 吕清毅 on 2018/10/4.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "HSSegmentController.h"
#import "HSPagerItem.h"
//#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
//#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define SELECTED_COLOR [UIColor redColor]

#define KTitleH 40


static NSString *ID=@"pager";

@interface HSSegmentController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, assign) CGFloat titleHeight;

@property (nonatomic, strong) NSMutableArray *itemArr;

@property(nonatomic,strong)NSMutableArray *textArr;

@property(nonatomic,strong)NSMutableArray *futextArr;

@property (nonatomic, weak) UIView *sliderView;
@property (nonatomic, strong) UICollectionView *pagerContainer;

@end

@implementation HSSegmentController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}
-(void)setTitleArr:(NSArray *)titleArr{
    _titleArr = titleArr;
    
    [self initTitleWrap];
    
}

-(void)setControllerArr:(NSArray *)controllerArr{
    _controllerArr=controllerArr;
    
    [self initPagerContainer];
}

-(void)initTitleWrap{
    
    CGFloat itemW = (CGFloat)SCREENWIDTH/_titleArr.count;
    //标题容器
    UIView *titleWrap;
    if (@available(iOS 11.0, *))
    {
        titleWrap = [[UIView alloc]initWithFrame:CGRectMake(0,TopBarHeight_xs, SCREENWIDTH, 40)];
    }
    else {
        titleWrap = [[UIView alloc]initWithFrame:CGRectMake(0,TopBarHeight, SCREENWIDTH, 40)];
        // Fallback on earlier versions
    }
    
    //    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0,titleWrap.frame.size.height-1,SCREENWIDTH,1)];
    //line.backgroundColor = Cuttingline;
    //    [titleWrap addSubview:line];
    //
    titleWrap.backgroundColor = TotalWhite;
    [self.view addSubview:titleWrap];
    _titleWrap = titleWrap;
    
    _itemArr = [NSMutableArray array];
    _textArr = [NSMutableArray array];
    _futextArr = [NSMutableArray array];
    for (int i = 0; i < self.titleArr.count; i++) {
        UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom];
        //未选中颜色
        itemButton.backgroundColor = [UIColor clearColor];
        itemButton.frame = CGRectMake(itemW*i,0, itemW, KTitleH);
        itemButton.tag = i;
        [itemButton addTarget:self action:@selector(scrollToSelectedIndex:) forControlEvents:UIControlEventTouchUpInside];
        [titleWrap addSubview:itemButton];
        
        //CGSize size = [TTKUtil sizeWithText:self.titleArr[i] Height:14 Font:FontMediumWithSize(14)];
        
        UILabel  *text = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,itemW,KTitleH)];
        text.text = _titleArr[i];
        text.textAlignment = NSTextAlignmentCenter;
        text.font = FontMediumWithSize(14);
        text.textColor = UIColorFromHex(0x999999);
        [itemButton addSubview:text];
        
        UILabel  *futext = [[UILabel alloc]initWithFrame:CGRectMake(0,0,itemW,KTitleH)];
        futext.text = _subtitleArr[i];
        futext.font = [UIFont fontWithName:@"Helvetica" size:14];
        futext.textAlignment = NSTextAlignmentCenter;
        futext.textColor = [UIColor orangeColor];
        [itemButton addSubview:futext];
        
        [_itemArr addObject:itemButton];
        [_textArr addObject:text];
        [_futextArr addObject:futext];
    }
    
    
    //添加滑块
    UIView *sliderV=[[UIView alloc]initWithFrame:CGRectMake(0, KTitleH-1, itemW, 1)];
    sliderV.backgroundColor= goldColor;
    [self.titleWrap addSubview:sliderV];
    //添加一个10横线
//    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 40, SCREENWIDTH, 10)];
//    lineView.backgroundColor = UIColorFromHex(0xF7F7F7);
//    [self.titleWrap addSubview:lineView];
//    sliderV.width = item.titleLabel.width;
//    self.indicatorView.centerX = button.centerX;
    _sliderView=sliderV;
    
    [self scrollToSelectedIndex:self.itemArr[0]];
}



-(void)initPagerContainer{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(SCREENWIDTH, SCREENHEIGHT);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, KTitleH+TopBarHeight, SCREENWIDTH, SCREENHEIGHT) collectionViewLayout:layout];
    
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    _pagerContainer = collectionView;
    collectionView.pagingEnabled = YES;
    collectionView.bounces = NO;
    collectionView.backgroundColor=[UIColor clearColor];
    collectionView.showsVerticalScrollIndicator=NO;
    [collectionView registerClass:[HSPagerItem class] forCellWithReuseIdentifier:ID];
    
}
-(void)selectToSelectedIndex:(int )buttonIdex{
    
    [self selectButton:buttonIdex];
    //滚动到选中页面
    [UIView animateWithDuration:0.3 animations:^{
        self.pagerContainer.contentOffset = CGPointMake(SCREENWIDTH*buttonIdex, 0);
    }];
    //保存选中索引
    self.oldSelectedIndex = buttonIdex;
}

-(void)scrollToSelectedIndex:(UIButton *) item{
    [self selectButton:item.tag];
    //滚动到选中页面
    [UIView animateWithDuration:0.3 animations:^{
        self.pagerContainer.contentOffset = CGPointMake(SCREENWIDTH*item.tag, 0);
    }];
    //保存选中索引
    self.oldSelectedIndex = item.tag;
}


//监听滚动事件判断当前拖动到哪一个了
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x / SCREENWIDTH;
    [self selectButton:index];
}


//选中按钮的处理
-(void)selectButton:(NSInteger )index{
    //取消选中上一个
    [self.itemArr[self.oldSelectedIndex] setSelected:NO];
    //选中当前
    [self.itemArr[index] setSelected:YES];
    CGFloat itemW = (CGFloat)SCREENWIDTH/_titleArr.count;
    
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.sliderView.frame=CGRectMake(itemW*index, KTitleH-1, itemW, 1);
        //未选中颜色
        [self.itemArr[self.oldSelectedIndex] setBackgroundColor:[UIColor clearColor]];
        //选中颜色
        //选中背景颜色
        //   [self.itemArr[index] setBackgroundColor:[UIColor grayColor]];
        
        [[self.itemArr[index] titleLabel] setFont:FontSemiboldSize(14)];
        
        
        //      [self.textArr[index] setFrame:CGRectMake((itemW-60)/2, 12, 60,40)];
        //      [self.textArr[_oldSelectedIndex] setFrame:CGRectMake((itemW-40)/2, 12, 40,40)];
        [self.textArr[self.oldSelectedIndex] setFont:FontMediumWithSize(14)];
        [self.textArr[self.oldSelectedIndex] setTextColor:UIColorFromHex(0x999999)];
        [self.textArr[index] setTextColor:titleColor];
        
        [self.textArr[index] setFont:FontSemiboldSize(14)];
        
        [self.futextArr[self.oldSelectedIndex] setFont:FontMediumWithSize(14)];
        [self.futextArr[index] setFont:FontSemiboldSize(14)];
        
    } completion:^(BOOL finished) {
        
    }];
    
    //记录当前选中
    self.oldSelectedIndex = index;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.controllerArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HSPagerItem  *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    UIViewController *tableVc = self.controllerArr[indexPath.item];
    cell.content = tableVc.view;
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
